home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / z.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-28  |  8.2 KB  |  406 lines

  1.  
  2. #include "qe3.h"
  3.  
  4. #define    PAGEFLIPS    2
  5.  
  6. z_t        z;
  7.  
  8. /*
  9. ============
  10. Z_Init
  11. ============
  12. */
  13. void Z_Init (void)
  14. {
  15.     z.origin[0] = 0;
  16.     z.origin[1] = 20;
  17.     z.origin[2] = 46;
  18.  
  19.     z.scale = 1;
  20. }
  21.  
  22.  
  23.  
  24. /*
  25. ============================================================================
  26.  
  27.   MOUSE ACTIONS
  28.  
  29. ============================================================================
  30. */
  31.  
  32. static    int    cursorx, cursory;
  33.  
  34. /*
  35. ==============
  36. Z_MouseDown
  37. ==============
  38. */
  39. void Z_MouseDown (int x, int y, int buttons)
  40. {
  41.     vec3_t    org, dir, vup, vright;
  42.     brush_t    *b;
  43.  
  44.     Sys_GetCursorPos (&cursorx, &cursory);
  45.  
  46.     vup[0] = 0; vup[1] = 0; vup[2] = 1/z.scale;
  47.  
  48.     VectorCopy (z.origin, org);
  49.     org[2] += (y - (z.height/2))/z.scale;
  50.     org[1] = -8192;
  51.  
  52.     b = selected_brushes.next;
  53.     if (b != &selected_brushes)
  54.     {
  55.         org[0] = (b->mins[0] + b->maxs[0])/2;
  56.     }
  57.  
  58.     dir[0] = 0; dir[1] = 1; dir[2] = 0;
  59.  
  60.     vright[0] = 0; vright[1] = 0; vright[2] = 0;
  61.  
  62.     // LBUTTON = manipulate selection
  63.     // shift-LBUTTON = select
  64.     // middle button = grab texture
  65.     // ctrl-middle button = set entire brush to texture
  66.     // ctrl-shift-middle button = set single face to texture
  67.     if ( (buttons == MK_LBUTTON)
  68.         || (buttons == (MK_LBUTTON | MK_SHIFT))
  69.         || (buttons == MK_MBUTTON)
  70. //        || (buttons == (MK_MBUTTON|MK_CONTROL))
  71.         || (buttons == (MK_MBUTTON|MK_SHIFT|MK_CONTROL)) )
  72.     {
  73.         Drag_Begin (x, y, buttons,
  74.             vright, vup,
  75.             org, dir);
  76.         return;
  77.     }
  78.  
  79.     // control mbutton = move camera
  80.     if ((buttons == (MK_CONTROL|MK_MBUTTON) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
  81.     {    
  82.         camera.origin[2] = org[2] ;
  83.         Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
  84.     }
  85.  
  86.  
  87. }
  88.  
  89. /*
  90. ==============
  91. Z_MouseUp
  92. ==============
  93. */
  94. void Z_MouseUp (int x, int y, int buttons)
  95. {
  96.     Drag_MouseUp ();
  97. }
  98.  
  99. /*
  100. ==============
  101. Z_MouseMoved
  102. ==============
  103. */
  104. void Z_MouseMoved (int x, int y, int buttons)
  105. {
  106.     if (!buttons)
  107.         return;
  108.     if (buttons == MK_LBUTTON)
  109.     {
  110.         Drag_MouseMoved (x, y, buttons);
  111.         Sys_UpdateWindows (W_Z|W_CAMERA);
  112.         return;
  113.     }
  114.     // rbutton = drag z origin
  115.     if (buttons == MK_RBUTTON)
  116.     {
  117.         Sys_GetCursorPos (&x, &y);
  118.         if ( y != cursory)
  119.         {
  120.             z.origin[2] += y-cursory;
  121.             Sys_SetCursorPos (cursorx, cursory);
  122.             Sys_UpdateWindows (W_Z);
  123.         }
  124.         return;
  125.     }
  126.         // control mbutton = move camera
  127.     if ((buttons == (MK_CONTROL|MK_MBUTTON) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
  128.     {    
  129.         camera.origin[2] = (y - (z.height/2))/z.scale;
  130.         Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
  131.     }
  132.  
  133. }
  134.  
  135.  
  136. /*
  137. ============================================================================
  138.  
  139. DRAWING
  140.  
  141. ============================================================================
  142. */
  143.  
  144.  
  145. /*
  146. ==============
  147. Z_DrawGrid
  148. ==============
  149. */
  150. void Z_DrawGrid (void)
  151. {
  152.     float    zz, zb, ze;
  153.     int        w, h;
  154.     char    text[32];
  155.  
  156.     w = z.width/2 / z.scale;
  157.     h = z.height/2 / z.scale;
  158.  
  159.     zb = z.origin[2] - h;
  160.     if (zb < region_mins[2])
  161.         zb = region_mins[2];
  162.     zb = 64 * floor (zb/64);
  163.  
  164.     ze = z.origin[2] + h;
  165.     if (ze > region_maxs[2])
  166.         ze = region_maxs[2];
  167.     ze = 64 * ceil (ze/64);
  168.  
  169.     // draw major blocks
  170.  
  171.     glColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR]);
  172.  
  173.     glBegin (GL_LINES);
  174.  
  175.     glVertex2f (0, zb);
  176.     glVertex2f (0, ze);
  177.  
  178.     for (zz=zb ; zz<ze ; zz+=64)
  179.     {
  180.         glVertex2f (-w, zz);
  181.         glVertex2f (w, zz);
  182.     }
  183.  
  184.     glEnd ();
  185.  
  186.     // draw minor blocks
  187.     if (g_qeglobals.d_showgrid && g_qeglobals.d_gridsize*z.scale >= 4)
  188.     {
  189.         glColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR]);
  190.  
  191.         glBegin (GL_LINES);
  192.         for (zz=zb ; zz<ze ; zz+=g_qeglobals.d_gridsize)
  193.         {
  194.             if ( ! ((int)zz & 63) )
  195.                 continue;
  196.             glVertex2f (-w, zz);
  197.             glVertex2f (w, zz);
  198.         }
  199.         glEnd ();
  200.     }
  201.  
  202.     // draw coordinate text if needed
  203.  
  204.     glColor4f(0, 0, 0, 0);
  205.  
  206.     for (zz=zb ; zz<ze ; zz+=64)
  207.     {
  208.         glRasterPos2f (-w+1, zz);
  209.         sprintf (text, "%i",(int)zz);
  210.         glCallLists (strlen(text), GL_UNSIGNED_BYTE, text);
  211.     }
  212. }
  213.  
  214. #define CAM_HEIGHT        48 // height of main part
  215. #define CAM_GIZMO        8    // height of the gizmo
  216.  
  217. void ZDrawCameraIcon (void)
  218. {
  219.     float    x, y;
  220.     int    xCam = z.width/4;
  221.  
  222.     x = 0;
  223.     y = camera.origin[2];
  224.  
  225.     glColor3f (0.0, 0.0, 1.0);
  226.     glBegin(GL_LINE_STRIP);
  227.     glVertex3f (x-xCam,y,0);
  228.     glVertex3f (x,y+CAM_GIZMO,0);
  229.     glVertex3f (x+xCam,y,0);
  230.     glVertex3f (x,y-CAM_GIZMO,0);
  231.     glVertex3f (x-xCam,y,0);
  232.     glVertex3f (x+xCam,y,0);
  233.     glVertex3f (x+xCam,y-CAM_HEIGHT,0);
  234.     glVertex3f (x-xCam,y-CAM_HEIGHT,0);
  235.     glVertex3f (x-xCam,y,0);
  236.     glEnd ();
  237.  
  238. }
  239.  
  240. GLbitfield glbitClear = GL_COLOR_BUFFER_BIT; //HACK
  241.  
  242. /*
  243. ==============
  244. Z_Draw
  245. ==============
  246. */
  247. void Z_Draw (void)
  248. {
  249.     brush_t    *brush;
  250.     float    w, h;
  251.     double    start, end;
  252.     qtexture_t    *q;
  253.     float    top, bottom;
  254.     vec3_t    org_top, org_bottom, dir_up, dir_down;
  255.     int xCam = z.width/3;
  256.  
  257.     if (!active_brushes.next)
  258.         return;    // not valid yet
  259.  
  260.     if (z.timing)
  261.         start = Sys_DoubleTime ();
  262.  
  263.     //
  264.     // clear
  265.     //
  266.     glViewport(0, 0, z.width, z.height);
  267.  
  268.     glClearColor (
  269.         g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
  270.         g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
  271.         g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
  272.         0);
  273.  
  274.     /* GL Bug */ 
  275.     /* When not using hw acceleration, gl will fault if we clear the depth 
  276.     buffer bit on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT
  277.     only after Z_Draw() has been called once. Yeah, right. */
  278.     glClear(glbitClear); 
  279.     glbitClear |= GL_DEPTH_BUFFER_BIT;
  280.  
  281.     glMatrixMode(GL_PROJECTION);
  282.  
  283.     glLoadIdentity ();
  284.     w = z.width/2 / z.scale;
  285.     h = z.height/2 / z.scale;
  286.     glOrtho (-w, w, z.origin[2]-h, z.origin[2]+h, -8, 8);
  287.  
  288.     glDisable(GL_TEXTURE_2D);
  289.     glDisable(GL_TEXTURE_1D);
  290.     glDisable(GL_DEPTH_TEST);
  291.     glDisable(GL_BLEND);
  292.  
  293.  
  294.     //
  295.     // now draw the grid
  296.     //
  297.     Z_DrawGrid ();
  298.  
  299.     //
  300.     // draw stuff
  301.     //
  302.  
  303.     glDisable(GL_CULL_FACE);
  304.  
  305.     glShadeModel (GL_FLAT);
  306.  
  307.     glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  308.  
  309.     glDisable(GL_TEXTURE_2D);
  310.     glDisable(GL_BLEND);
  311.     glDisable(GL_DEPTH_TEST);
  312.  
  313.  
  314.     // draw filled interiors and edges
  315.     dir_up[0] = 0 ; dir_up[1] = 0; dir_up[2] = 1;
  316.     dir_down[0] = 0 ; dir_down[1] = 0; dir_down[2] = -1;
  317.     VectorCopy (z.origin, org_top);
  318.     org_top[2] = 4096;
  319.     VectorCopy (z.origin, org_bottom);
  320.     org_bottom[2] = -4096;
  321.  
  322.     for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)
  323.     {
  324.         if (brush->mins[0] >= z.origin[0]
  325.             || brush->maxs[0] <= z.origin[0]
  326.             || brush->mins[1] >= z.origin[1]
  327.             || brush->maxs[1] <= z.origin[1])
  328.             continue;
  329.  
  330.         if (!Brush_Ray (org_top, dir_down, brush, &top))
  331.             continue;
  332.         top = org_top[2] - top;
  333.         if (!Brush_Ray (org_bottom, dir_up, brush, &bottom))
  334.             continue;
  335.         bottom = org_bottom[2] + bottom;
  336.  
  337.         q = Texture_ForName (brush->brush_faces->texdef.name);
  338.         glColor3f (q->color[0], q->color[1], q->color[2]);
  339.         glBegin (GL_QUADS);
  340.         glVertex2f (-xCam, bottom);
  341.         glVertex2f (xCam, bottom);
  342.         glVertex2f (xCam, top);
  343.         glVertex2f (-xCam, top);
  344.         glEnd ();
  345.  
  346.         glColor3f (1,1,1);
  347.         glBegin (GL_LINE_LOOP);
  348.         glVertex2f (-xCam, bottom);
  349.         glVertex2f (xCam, bottom);
  350.         glVertex2f (xCam, top);
  351.         glVertex2f (-xCam, top);
  352.         glEnd ();
  353.     }
  354.  
  355.     //
  356.     // now draw selected brushes
  357.     //
  358.     for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  359.     {
  360.         if ( !(brush->mins[0] >= z.origin[0]
  361.             || brush->maxs[0] <= z.origin[0]
  362.             || brush->mins[1] >= z.origin[1]
  363.             || brush->maxs[1] <= z.origin[1]) )
  364.         {
  365.             if (Brush_Ray (org_top, dir_down, brush, &top))
  366.             {
  367.                 top = org_top[2] - top;
  368.                 if (Brush_Ray (org_bottom, dir_up, brush, &bottom))
  369.                 {
  370.                     bottom = org_bottom[2] + bottom;
  371.  
  372.                     q = Texture_ForName (brush->brush_faces->texdef.name);
  373.                     glColor3f (q->color[0], q->color[1], q->color[2]);
  374.                     glBegin (GL_QUADS);
  375.                     glVertex2f (-xCam, bottom);
  376.                     glVertex2f (xCam, bottom);
  377.                     glVertex2f (xCam, top);
  378.                     glVertex2f (-xCam, top);
  379.                     glEnd ();
  380.                 }
  381.             }
  382.         }
  383.  
  384.         glColor3f (1,0,0);
  385.         glBegin (GL_LINE_LOOP);
  386.         glVertex2f (-xCam, brush->mins[2]);
  387.         glVertex2f (xCam, brush->mins[2]);
  388.         glVertex2f (xCam, brush->maxs[2]);
  389.         glVertex2f (-xCam, brush->maxs[2]);
  390.         glEnd ();
  391.     }
  392.  
  393.  
  394.     ZDrawCameraIcon ();
  395.  
  396.     glFinish();
  397.     QE_CheckOpenGLForErrors();
  398.  
  399.     if (z.timing)
  400.     {
  401.         end = Sys_DoubleTime ();
  402.         Sys_Printf ("z: %i ms\n", (int)(1000*(end-start)));
  403.     }
  404. }
  405.  
  406.